package com.sina.auto.components.AddContentImpl;

import java.awt.*;
import java.awt.image.BufferedImage;

import static com.sina.auto.components.AddContentImpl.ImageUtil.MergeImage.mergeImage;
import static com.sina.auto.components.AddContentImpl.ImageUtil.*;
import static java.awt.Color.black;

class AddString {
    /**
     * 添加标题和作者
     * @param img
     * @param content
     * @param author
     * @param title
     * @return
     * @throws Exception
     */
    static BufferedImage addString(BufferedImage img, String content, String author, String title) throws Exception {

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);

        int realWidth = (int) (width - (width / 5.2));
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);
        Font titleFont = new  Font("黑体", Font.BOLD, width / 15);
        Font timeFont = new Font("黑体", Font.BOLD, width / 32);

        int startHeight = (fontHeight(contentFont) * 2 + fontHeight(titleFont));

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + fontHeight(contentFont) + fontHeight(titleFont));

        //绘制标题
        drawTitle(width, titleFont, image, title, fontHeight(contentFont));

        //绘制时间
        drawTime(width, timeFont, image, author, fontHeight(contentFont) *2);

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];
        //将文字储存进上方的字符串数组中
        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);
        //绘制数组中的字符串
        drawContent(rowSpacing, contentFont, width, startHeight, g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 仅正文和时间
     * @param img
     * @param content
     * @return
     * @throws Exception
     */
    static BufferedImage addString(BufferedImage img, String content) throws Exception {

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);

        int realWidth = (int) (width - (width / 5.2));
        //决定行数，向上取整
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);
        Font timeFont = new Font("黑体", Font.BOLD, width / 32);

        int startHeight = (fontHeight(contentFont) * 2);

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + fontHeight(contentFont));

        //绘制时间
        drawTime(width, timeFont, image, fontHeight(contentFont));

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];

        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);

        drawContent(rowSpacing, contentFont, width, startHeight, g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 添加标题
     * @param img
     * @param content
     * @param title
     * @return
     * @throws Exception
     */
    static BufferedImage addStringOnlyTitle(BufferedImage img, String content, String title) throws Exception {

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);

        int realWidth = (int) (width - (width / 5.2));
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);
        Font titleFont = new  Font("黑体", Font.BOLD, width / 15);
        Font timeFont = new Font("黑体", Font.BOLD, width / 32);

        int startHeight = (fontHeight(contentFont) * 2 + fontHeight(titleFont));

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + fontHeight(contentFont) + fontHeight(titleFont));

        //绘制标题
        drawTitle(width, titleFont, image, title, fontHeight(contentFont));

        //绘制时间
        drawTime(width, timeFont, image, fontHeight(contentFont) *2);

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];

        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);

        drawContent(rowSpacing, contentFont, width, startHeight, g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 添加作者
     * @param img
     * @param content
     * @param author
     * @return
     * @throws Exception
     */
    static BufferedImage addStringOnlyAuthor(BufferedImage img, String content, String author) throws Exception {

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);

        int realWidth = (int) (width - (width / 5.2));
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);
        Font titleFont = new  Font("黑体", Font.BOLD, width / 15);
        Font timeFont = new Font("黑体", Font.BOLD, width / 32);

        int startHeight = (fontHeight(contentFont) * 2);

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + fontHeight(contentFont));

        //绘制时间
        drawTime(width, timeFont, image, author, fontHeight(contentFont));

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];

        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);

        drawContent(rowSpacing, contentFont, width, startHeight, g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 根据计算字符串数组每个字符串元素最后一行留白的空间，将空白符添加到字符串末尾。
     * @param img
     * @param Content
     * @return
     * @throws Exception
     */
    static BufferedImage addString(BufferedImage img, String[] Content) throws Exception {
        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);
        int realWidth = (int) (width - (width / 5.2));

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int realRowWidth = fontWidth(contentFont,"怎么会有这么标准的十四个字呢");

        String content = "";
        int a = 0;
        for(int i = 0; i < Content.length-1; i++) {
                 a = (realRowWidth - (fontWidth(contentFont, Content[i]) - (fontWidth(contentFont, Content[i]) / realRowWidth) * realRowWidth)) / fontWidth(contentFont, "  ");
            StringBuffer blank = new StringBuffer();
            for(int j = 0;j<a*2;j++){
                blank.append(" ");
            }
            Content[i] =  Content[i] + blank.toString();
        }

        for(int k = 0; k < Content.length; k++ ){
            content += Content[k];
        }

        //计算行数，向上取整
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);
        Font timeFont = new Font("黑体", Font.BOLD, width / 32);

        int startHeight = (70 + fontHeight(timeFont) + rowSpacing*2 );

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + startHeight + rowSpacing);

        //绘制时间
        drawTime(width, timeFont, image, 70);

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];

        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);

        drawContent(rowSpacing, contentFont, width, startHeight, g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 仅添加正文，不添加时间作者和标题
     * @param img
     * @param content
     * @return
     * @throws Exception
     */
    private static BufferedImage addOnlyString(BufferedImage img, String content) throws Exception {

        //设定字体大小和行距
        int fontSize = (int) (img.getWidth()/18.6);
        int rowSpacing = (int) (img.getWidth()/68);
        Font contentFont = new Font("黑体", Font.BOLD, fontSize);

        int width = img.getWidth(null) == -1 ? 200 : img.getWidth(null);

        int realWidth = (int) (width - (width / 5.2));
        //决定行数，向上取整
        int lineNum = (int) Math.ceil(fontWidth(contentFont,content) * 1.0 / realWidth);

        BufferedImage image = imageStretch(img,width,lineNum * (fontHeight(contentFont) + rowSpacing) + rowSpacing);

        //获得画笔，绘制正文
        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(black);
        g2.setBackground(Color.red);
        g2.drawImage(image, 0, 0, null);
        g2.setFont(contentFont);

        FontMetrics metrics = g2.getFontMetrics();

        StringBuilder stringBuilder = new StringBuilder();
        int j = 0;
        int tempStart = 0;
        String tempStrings[] = new String[lineNum];//存储换行之后每一行的字符串
        String extendTemp[] = new String[lineNum + 1];

        stringHeight(content, metrics, realWidth, stringBuilder, j, tempStart, tempStrings, extendTemp);

        drawContent(rowSpacing, contentFont, width, fontHeight(contentFont), g2, tempStrings, extendTemp);
        g2.dispose();
        return image;
    }

    /**
     * 数组中每个字符串生成一张图片，最后将所有图片拼接到一起。
     * @param image
     * @param content
     * @return
     * @throws Exception
     */
    static BufferedImage addParagByMerge(BufferedImage image, String[] content) throws Exception {
        BufferedImage[] bufferedImages = new BufferedImage[content.length];
        for(int i = 0; i<content.length;i++){
            if(i == 0){
                bufferedImages[i] = addString(image,content[i]);
            }else {
                bufferedImages[i] = addOnlyString(image, content[i]);
            }
        }
        return mergeImage(bufferedImages,2);
    }

    /**
     * 多段落绘制，添加作者
     * @param image
     * @param content
     * @param author
     * @return
     * @throws Exception
     */
    static BufferedImage addParagByMergeAddAuthor(BufferedImage image, String[] content, String author) throws Exception {
        BufferedImage[] bufferedImages = new BufferedImage[content.length];
        for(int i = 0; i<content.length;i++){
            if(i == 0){
                bufferedImages[i] = addStringOnlyAuthor(image,content[i],author);
            }else {
                bufferedImages[i] = addOnlyString(image, content[i]);
            }
        }
        return mergeImage(bufferedImages,2);
    }

    /**
     * 多段落绘制，添加标题
     * @param image
     * @param content
     * @param title
     * @return
     * @throws Exception
     */
    static BufferedImage addParagByMergeAddTiltle(BufferedImage image, String[] content, String title) throws Exception {
        BufferedImage[] bufferedImages = new BufferedImage[content.length];
        for(int i = 0; i<content.length;i++){
            if(i == 0){
                bufferedImages[i] = addStringOnlyTitle(image,content[i],title);
            }else {
                bufferedImages[i] = addOnlyString(image, content[i]);
            }
        }
        return mergeImage(bufferedImages,2);
    }

    /**
     * 多段落绘制，添加标题与作者
     * @param image
     * @param content
     * @param author
     * @param title
     * @return
     * @throws Exception
     */
    static BufferedImage addParagByMerge(BufferedImage image, String[] content, String author, String title) throws Exception {
        BufferedImage[] bufferedImages = new BufferedImage[content.length];
        for(int i = 0; i<content.length;i++){
            if(i == 0){
                bufferedImages[i] = addString(image,content[i],author,title);
            }else {
                bufferedImages[i] = addOnlyString(image, content[i]);
            }
        }
        return mergeImage(bufferedImages,2);
    }
}
